package com.industrial.premu.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import com.industrial.common.core.utils.uuid.IdUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.premu.domain.PreMuMessage;
import com.industrial.premu.service.IPreMuMessageService;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.page.TableDataInfo;

/**
 * 诉求反馈Controller
 * 
 * @author Zhou_S
 * @date 2024-02-04
 */
@RestController
@RequestMapping("/message")
public class PreMuMessageController extends BaseController
{
    @Autowired
    private IPreMuMessageService preMuMessageService;

    /**
     * 查询诉求反馈列表
     */
    @RequiresPermissions("premu:message:list")
    @GetMapping("/list")
    public TableDataInfo list(PreMuMessage preMuMessage)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("q_01_001")){
            preMuMessage.setSendId(loginUser.getUsername());
        }else if(roles.contains("x_01_011")){
            preMuMessage.setCounty(loginUser.getSysUser().getDistrict());
        }else {
            logger.info("无此查看权限！");
        }

        startPage();
        List<PreMuMessage> list = preMuMessageService.selectPreMuMessageList(preMuMessage);
        return getDataTable(list);
    }

    /**
     * 导出诉求反馈列表
     */
    @RequiresPermissions("premu:message:export")
    @Log(title = "诉求反馈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreMuMessage preMuMessage)
    {
        List<PreMuMessage> list = preMuMessageService.selectPreMuMessageList(preMuMessage);
        ExcelUtil<PreMuMessage> util = new ExcelUtil<PreMuMessage>(PreMuMessage.class);
        util.exportExcel(response, list, "诉求反馈数据");
    }

    /**
     * 获取诉求反馈详细信息
     */
    @RequiresPermissions("premu:message:query")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(preMuMessageService.selectPreMuMessageByBaseId(baseId));
    }

    /**
     * 新增诉求反馈
     */
    @RequiresPermissions("premu:message:add")
    @Log(title = "诉求反馈", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PreMuMessage preMuMessage)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        preMuMessage.setCounty(loginUser.getSysUser().getDistrict());
        preMuMessage.setBaseId(IdUtils.simpleUUID());
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        preMuMessage.setSendTime(format.format(new Date()));
        preMuMessage.setSendId(loginUser.getUsername());
        return toAjax(preMuMessageService.insertPreMuMessage(preMuMessage));
    }

    /**
     * 修改诉求反馈
     */
    @RequiresPermissions("premu:message:edit")
    @Log(title = "诉求反馈", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PreMuMessage preMuMessage)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("x_01_011")){
            preMuMessage.setReceiveId(loginUser.getUserid());
        }else {
            logger.info("无此查看权限！");
        }
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        preMuMessage.setReceiveTime(format.format(new Date()));
        return toAjax(preMuMessageService.updatePreMuMessage(preMuMessage));
    }

    /**
     * 删除诉求反馈
     */
    @RequiresPermissions("premu:message:remove")
    @Log(title = "诉求反馈", businessType = BusinessType.DELETE)
	@DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(preMuMessageService.deletePreMuMessageByBaseIds(baseIds));
    }
    @RequiresPermissions("premu:message:deleteByBaseId")
    @DeleteMapping("/deleteByBaseId")
    public AjaxResult deleteByBaseId(String baseId)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("q_01_001")){
            return toAjax(preMuMessageService.updateByBaseId(baseId));
        }else{
            return  warn("无此权限！");
        }
    }
}
